package com.lx.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lx.mapper.TeamMapper;
import com.lx.pojo.Team;
import com.lx.pojo.TeamExample;
import com.lx.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 零巽
 */
@Service
public class TeamService {
    @Resource
    private TeamMapper teamMapper;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Team> queryByPage(Integer pageNumber, Integer pageSize, QueryVo vo){
        //查询条件
        TeamExample example = new TeamExample();
        //创建查询的条件容器
        TeamExample.Criteria criteria = example.createCriteria();
        //根据数据添加条件
        if (vo != null) {
            if(vo.getTeamName()!=null&&!"".equals(vo.getTeamName().trim())){
                criteria.andTeamNameLike("%"+vo.getTeamName().trim()+"%");
            }
            if(vo.getChineseName()!=null&&!"".equals(vo.getChineseName().trim())){
                criteria.andChineseNameLike("%"+vo.getChineseName().trim()+"%");
            }
            if(vo.getCoach()!=null&&!"".equals(vo.getCoach().trim())){
                criteria.andCoachLike("%"+vo.getCoach().trim()+"%");
            }
            if (vo.getBeginDate() != null) {
                criteria.andCreateTimeGreaterThanOrEqualTo(vo.getBeginDate());
            }
            if (vo.getEndDate() != null) {
                criteria.andCreateTimeLessThanOrEqualTo(vo.getEndDate());
            }
            if (vo.getArea() != null && vo.getArea()!=-1) {
                criteria.andAreaEqualTo(vo.getArea());
            }
        }
        //添加分页条件 只对紧邻的下一行sql语句生效
        PageHelper.startPage(pageNumber, pageSize);
        //添加完条件后进行数据查询
        List<Team> teams = teamMapper.selectByExample(example);
        //将查询到的结果封装进PageInfo类中并返回
        return new PageInfo<>(teams);
    }

    /**
     * 添加
     * @param team
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addTeam(Team team) {
        return teamMapper.insertSelective(team);
    }

    /**
     * 根据主键查询
     * @param teamId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Team queryByTeamId(Integer teamId) {
        return teamMapper.selectByPrimaryKey(teamId);
    }

    /**
     * 根据主键动态更新球队
     * @param team
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateTeam(Team team) {
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    /**
     * 根据主键删除球队
     * @param teamId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteTeam(int teamId){
        Team team = teamMapper.selectByPrimaryKey(teamId);
        team.setIsDel(1);
        return teamMapper.updateByPrimaryKeySelective(team);
    }
}
